package test.merge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import test.merge.domain.Order;
import test.merge.domain.Stock;
import test.merge.mapper.OrderMapper;
import test.merge.mapper.StockMapper;
import test.merge.service.IStockService;

import java.util.List;

/**
 * @author ：张任豪
 * @date ：2022/7/19 18:29
 * @description：
 */
@AllArgsConstructor
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements IStockService {

    private StockMapper stockMapper;

    @Override
    public Stock getByGoodsId(String goodsid) {
        return stockMapper.selectOne(new QueryWrapper<Stock>().eq("goodsId",goodsid));
    }

    @Override
    public boolean decStock(String goodsId, Integer number) {
        Stock stock = getByGoodsId(goodsId);
        boolean flea = stock.getNumber() - number >= 0;
        if (flea){
            update(new UpdateWrapper<Stock>().set("number",stock.getNumber() - number).eq("goodsId",goodsId));
        }
        return flea;
    }

    @Override
    public List<Stock> queryAll() {
        return list();
    }
}
